﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace TARADesktopDoctor
{
    /// <summary>
    /// Interaction logic for TreatmentItem.xaml
    /// </summary>
    public partial class TreatmentItem : UserControl
    {
        tara_wcf.ItaraServiceClient svc = new tara_wcf.ItaraServiceClient();
        tara_wcf.EXE treatment = new tara_wcf.EXE();
        public static tara_wcf.EXE assignedSetting = new tara_wcf.EXE();
        tara_wcf.EXE assignedList = new tara_wcf.EXE();
        public static bool updateSetting = false;

        public TreatmentItem(tara_wcf.EXE exe, tara_wcf.EXE assignedSetting, bool assigned)
        {
            this.InitializeComponent();
            treatment = exe;
            lblName.Text = treatment.Name;
            lblDescription.Text = treatment.Description;
            
            if (assigned == false)  //Game not assigned to patient
            {
                btnUnassign.Visibility = Visibility.Collapsed;
                if (treatment.ImageByte != null)
                {
                    imgTreatmentPic.Source = LoadImage(treatment.ImageByte);
                }
                updateSetting = false;
            }
            else //Game assigned to patient
            {
                btnUnassign.Visibility = Visibility.Visible;
                btnSettings.Visibility = Visibility.Visible;
                updateSetting = true;
                assignedList = assignedSetting;
                if (assignedSetting.ImageByte != null)
                {
                    imgTreatmentPic.Source = LoadImage(assignedSetting.ImageByte);
                }
                MainWindow main = App.Current.MainWindow as MainWindow;
                MainPage page = main.frame1.Content as MainPage;
                //Load the previous settings of the game
                page.TreatmentSettings.treatment = assignedSetting;
            }
        }

        #region buttons
        private void btnAssign_Click(object sender, RoutedEventArgs e)
        {
            btnAssign.Visibility = Visibility.Collapsed;
            btnUnassign.Visibility = Visibility.Visible;
            btnSettings.Visibility = Visibility.Visible;
            //add to patient's assigned treatment list
        }

        private void btnUnassign_Click(object sender, RoutedEventArgs e)
        {
            //remove from patient's assigned treatment list
            if (svc.unassignedGameToPatient(MainPage.patientDetail.Uid, treatment.Uid) == true)
            {
                //Remove from MainPage.assignedList
                foreach (tara_wcf.EXE temp in MainPage.assignedList)
                {
                    if (assignedList.Uid == treatment.Uid)
                    {
                        MainPage.assignedList.Remove(temp);
                        btnAssign.Visibility = Visibility.Visible;
                        btnUnassign.Visibility = Visibility.Collapsed;
                        btnSettings.Visibility = Visibility.Collapsed;
                        break;
                    }
                }
            }
        }

        private void btnInfo_Click(object sender, RoutedEventArgs e)
        {
            //show treatment info UC
            MainWindow main = App.Current.MainWindow as MainWindow;
            MainPage page = main.frame1.Content as MainPage;
            page.TreatmentInfo.treatment = this.treatment;
            page.TreatmentInfo.Show();
        }

        private void btnSettings_Click(object sender, RoutedEventArgs e)
        {
            //show treatment settings UC
            MainWindow main = App.Current.MainWindow as MainWindow;
            MainPage page = main.frame1.Content as MainPage;
            if (updateSetting == true)
            {
                page.TreatmentSettings.treatment = assignedList;
            }
            else
            {
                page.TreatmentSettings.treatment = this.treatment;
            }
            page.TreatmentSettings.Show();
        }
        #endregion

        private void ResetSelectionColors()
        {
            if (this.Parent != null)
            {
                if (this.Parent is StackPanel)
                {
                    StackPanel treatmentPanel = (StackPanel)this.Parent;
                    foreach (UIElement element in treatmentPanel.Children)
                    {
                        if (element is TreatmentItem)
                        {
                            TreatmentItem ai = (TreatmentItem)element;
                            ai.lblName.Foreground = Brushes.Black;
                            ai.lblDescription.Foreground = Brushes.Black;
                            ai.Background = null;
                        }
                    }
                }
            }
        }

        private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
           // ResetSelectionColors();
           // this.lblName.Foreground = Brushes.White;
           // this.lblDescription.Foreground = Brushes.White;
            //this.Background = new SolidColorBrush(Color.FromArgb(255, 47, 113, 204));
        }

        private void Rectangle_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			 ResetSelectionColors();
            this.lblName.Foreground = Brushes.White;
            this.lblDescription.Foreground = Brushes.White;
            this.Background = new SolidColorBrush(Color.FromArgb(255, 47, 113, 204));
        }

        private void LayoutRoot_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
	        ResetSelectionColors();
            this.lblName.Foreground = Brushes.White;
            this.lblDescription.Foreground = Brushes.White;
            this.Background = new SolidColorBrush(Color.FromArgb(255, 47, 113, 204));
        }

        BitmapSource LoadImage(Byte[] imageData)
        {
            using (MemoryStream ms = new MemoryStream(imageData))
            {
                var decoder = BitmapDecoder.Create(ms,
                    BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                return decoder.Frames[0];
            }
        }
    }
}